<?php
class Mc extends MY_Controller {
	
	var $currentPage = 'mc';
	var $caption = 'Mc';

	public function index() {

		$this->page();
	}

	
	function signup() {
		
		$this->load->library('form_validation');
		
		//$data['url'] = "http://localhost/mcchampdeals/mc";
		
		//$this->parser->parse('mcchamp/signup', $data);
		$this->page("signup" , array()); 
	}
	
	function saveuser() {
		
		$this->load->library('form_validation');
		$this->load->library('encrypt');
		$this->load->library('email');
		
		$username = $this->form_validation->set_rules('username','Username','trim|required|min_length[6]|xss_clean');
		$this->form_validation->set_rules('password','Password','required|min_length[8]|xss_clean');
		$this->form_validation->set_rules('firstname','Firstname','trim|required|xss_clean');
		$this->form_validation->set_rules('middlename','Middlename','trim|required|xss_clean');
		$this->form_validation->set_rules('lastname','Lastname','trim|required|xss_clean');
		$this->form_validation->set_rules('email','Email','trim|required|valid_email|xss_clean');
		$this->form_validation->set_rules('address','Address','trim|required|xss_clean');

		
		if($this->form_validation->run()==FALSE) {
			$this->page("signup" , ""); 
		} else {
			
			$user = $this->mUser;
			$user->username = set_value('username');
			$user->password = crypt(set_value('password'),PASSWORDSALT);
			$user->firstname = set_value('firstname');
			$user->lastname = set_value('lastname');
			$user->middlename = set_value('middlename');
			$user->email = set_value('email');
			$user->address = set_value('address');
			$user->activation_key = $this->encrypt->encode(microtime(true));
			
			if(count($user->findByUsername($user->username)) > 0) {
				$data['message'] = 'username already exist.';
				//$this->load->view('mcchamp/signup',$data);
				$this->page("signup" , $data); 
				return;
			}
			
			$user->insert();
			
			$activationLink = base_url() . 'mc/activate?key=' . $user->activation_key;
			$message = "Thank you for registering<br />";
			$message .= "Click the link to activate your account";
			$message .= "<a href='$activationLink'>HERE</a>";	
			$this->email->send_email('dthscyth@yahoo.com',$user->email,'subject',$message);
			$data['user'] = $user;
			//echo $this->email->print_debugger();
			//$this->load->view('mcchamp/thankyou',$data);
			$this->page("thankyou" , $data ); 
		}
	}
	
	function activate() {
		$key = $this->input->get('key',TRUE);
		
		$user = $this->mUser->findByActivationKey($key);
		
		if(count($user) > 0) {
			$data['message'] = 'your account is now activated';
			$user = $user[0];
			$this->db->update($this->mUser->table(),array('activated' => 1), array('id' => $user->id));
		} else {
			$data['message'] = 'invalid activation code';
		}
		
		$this->page("thankyou" , $data ); 
	}
	
	
	
	function account($accountpage = "" , $anyId = ""){
		
		$this->load->library('form_validation');
		
		//load account.php
		//inside this file there is a include , include page will be based from passed accountpage()
		//echo "ISSSSEEET ..... ".isset($_POST);
		if(  $accountpage == "saveaddressbook"){
			$accountpage = "addressbook";
			//echo "ISSET.......................".isset($_POST)."...".count($_POST);
			if(count($_POST) > 0){
			$this->mAddressBook->company = $_POST['company'];
			$this->mAddressBook->firstname = $_POST['firstname'];
			$this->mAddressBook->lastname = $_POST['lastname'];
			$this->mAddressBook->address1 = $_POST['address1'];
			$this->mAddressBook->address2 = $_POST['address2'];
			$this->mAddressBook->city = $_POST['city'];
			$this->mAddressBook->postalcode = $_POST['postalcode'];
			$this->mAddressBook->country = $_POST['country'];
			$this->mAddressBook->region = $_POST['region'];
			
				if($this->session->userdata('client_id')!=null ){
					$this->mAddressBook->user_id = $this->session->userdata('client_id');
					
					$addressbook = $this->mAddressBook->find_by_user($this->session->userdata('client_id'));
					
					
					if($addressbook == null)
						$this->mAddressBook->insert();
					else{
						$this->mAddressBook->id = $addressbook[0]->id;
						$this->mAddressBook->update();
					}
						
				
			}
			
			if($this->session->userdata('client_id')!=null ){
					$addressbook = $this->mAddressBook->find_by_user($this->session->userdata('client_id'));
					$data['addressbook'] = $addressbook[0];
			}
		}
		}
		
		if($accountpage == 'addressbook' && $this->session->userdata('client_id')!=null || $accountpage == "orderedealdetails"){
				$addressbook = $this->mAddressBook->find_by_user($this->session->userdata('client_id'));
				$data['addressbook'] = $addressbook[0];
		}
		
		if($accountpage == "editaccountinfo" || $accountpage == "orderedealdetails"){
			if(count($_POST) > 0){
				//ECHO "HEREEEEEEEEEEEEEEEEEEEEE";
				$this->mUser->updateClientAccountInfo();
				
			}
			$findUser = $this->mUser->getEntryById($this->session->userdata('client_id'));
			$data['currentUser'] = $findUser[0];
		}
		
		if($accountpage == "orderedealdetails"){
			$orders = $this->mOrderedDeal->findById($anyId);
			$data['order']  = $orders[0];
			
			$individualdeal = $this->mDeal->viewDealDetails($orders[0]->deal_id);
			if(count($individualdeal) > 0)
				$data['individualdeal'] = $individualdeal[0];
		}
		
		//findByUser
		
		if($accountpage == "orderhistory"){
			
			$data['orders'] = $this->mOrderedDeal->findByUser();
			
			
		}
		
		
		
		if($accountpage == "changepassword"){
			$findUser = $this->mUser->getEntryById($this->session->userdata('client_id'));
			if(count($_POST) > 0 && count($findUser)>0){
				
				$data = $_POST;
				
				$oldpassword =  crypt($this->input->post('oldpassword',TRUE),PASSWORDSALT);
				$retypedoldpassword =  crypt($this->input->post('retypedoldpassword',TRUE),PASSWORDSALT);
				
				$newpassword =  crypt($this->input->post('newpassword',TRUE),PASSWORDSALT);
				$retypednewpassword =  crypt($this->input->post('retypednewpassword',TRUE),PASSWORDSALT);
				$data['message'] = "Password was not updated";
				if($findUser[0]->password == $oldpassword && $oldpassword == $retypedoldpassword && $newpassword == $retypednewpassword){
					$this->mUser->changePassword($findUser[0]->id , $newpassword);
					$data['message'] = "Password was Successfully Updated";
				}
				
				$data['currentUser'] = $findUser[0];
			}
		}
		
		$data['accountpage'] = $accountpage;
		
		
		$this->page("account" , $data); 
	}
	

	public function logout(){

		$this->session->unset_userdata('client_id');
		$this->session->unset_userdata('client_fullname');
		$this->session->unset_userdata('client_email');
		$this->session->unset_userdata('client_type');
		
		$this->page();
	}
	
}

?>